﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System.Text.RegularExpressions;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    public class PageSettingsPage : WatiNBase
    {
        #region Constructors

        public PageSettingsPage(WatiNBase watinBase) : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName) { }

        #endregion

        #region Public Properties

        #region TextFields
        public TextField PageNameField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("ctr_ManageTabs_txtTabName")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("ctr_ManageTabs_txtTabName")));
            }
        }
        public TextField PageTitleField
        {
            get
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("ctr_ManageTabs_txtTitle")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("ctr_ManageTabs_txtTitle")));
            }
        }
        public TextField UserPermissionField
        {
            get { return UserPermissionsDiv.TextField(Find.ById("dnn_ctr_ManageTabs_dgPermissions_txtUser")); }
        }
        public TextField UrlTextField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("ctr_ManageTabs_ctlURL_txtUrl")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("ctr_ManageTabs_ctlURL_txtUrl")));
            }
        }
        public TextField StartDateTextField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("ctr_ManageTabs_datepickerStartDate_dateInput_text")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("ctr_ManageTabs_datepickerStartDate_dateInput_text")));
            }
        }
        public TextField EndDateTextField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("ctr_ManageTabs_datepickerEndDate_dateInput_text")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("ctr_ManageTabs_datepickerEndDate_dateInput_text")));
            }
        }
        #endregion

        #region SelectLists
        public SelectList ParentPageSelection
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("ctr_ManageTabs_cboParentTab")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("ctr_ManageTabs_cboParentTab")));
            }
        }
        public SelectList PageTemplateSelect
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("ctr_ManageTabs_cboTemplate")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("ctr_ManageTabs_cboTemplate")));
            }
        }
        public SelectList PageSkinSelect
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("ManageTabs_pageSkinCombo")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("ManageTabs_pageSkinCombo")));
            }
        }
        public SelectList PageContainerSelect
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("ManageTabs_pageContainerCombo")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("ManageTabs_pageContainerCombo")));
            }
        }
        public SelectList CopyPageSelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("ctr_ManageTabs_cboCopyPage")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("ctr_ManageTabs_cboCopyPage")));
            }
        }
        public SelectList LinkedPageSelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("ManageTabs_ctlURL_cboTabs")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("ManageTabs_ctlURL_cboTabs")));
            }
        }
        public SelectList InsertPageSelectList
        {
            get { return IEInstance.SelectList(Find.ById(s => s.EndsWith("dnn_ctr_ManageTabs_cboPositionTab"))); }
        }
        public SelectList FileNameSelectList
        {
            get { return IEInstance.SelectList(Find.ById(s => s.EndsWith("ctr_ManageTabs_ctlURL_cboFiles"))); }
        }
        public SelectList TemplateFolderSelectList
        {
            get 
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("ctr_ManageTabs_cboFolders")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("ctr_ManageTabs_cboFolders")));
            }
        }
        public SelectList IconFileLocationSelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("ManageTabs_ctlIcon_cboFolders")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("ManageTabs_ctlIcon_cboFolders")));
            }
        }
        public SelectList IconFileNameSelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("ManageTabs_ctlIcon_cboFiles")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("ManageTabs_ctlIcon_cboFiles")));
            }
        }
        #endregion

        #region CheckBoxes
        public CheckBox AddedUserEditPermissionCheckBox
        {
            get { return UserPermissionsDiv.TableRows.Filter(Find.ByClass("DataGrid_Item"))[3].CheckBoxes[1]; }
        }
        public TextField AllUsersViewPermissionCheckbox
        {
            get
            {
                return UserPermissionsDiv.TextFields.Filter(Find.ByName(s => s.Contains("$ManageTabs$dgPermissions")))[2];
            }
        }
        public CheckBox IncludeInMenuCheckbox
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.CheckBox(Find.ById(s => s.EndsWith("ctr_ManageTabs_chkMenu")));
                }
                return IEInstance.CheckBox(Find.ById(s => s.EndsWith("ctr_ManageTabs_chkMenu")));
            }
        }
        #endregion

        #region Links
        public Link AddPageLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByText(s => s.Contains("Add Page")));
                }
                return ContentPane.Link(Find.ByText(s => s.Contains("Add Page")));
            }
        }
        public Link UpdatePageLink
        {
            get
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByText(s => s.Contains("Update Page")));
                }
                return ContentPane.Link(Find.ByText(s => s.Contains("Update Page")));
            }
        }
        public Link DeletePageLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByTitle("Delete"));
                }
                return IEInstance.Link(Find.ByTitle("Delete"));
            }
        }
        public Link AddUserPermissionLink
        {
            get { return UserPermissionsDiv.Link(Find.ByText(s => s.Contains("Add"))); }
        }
        public Link LocalizationTabLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByText("Localization"));
                }
                return ContentPane.Link(Find.ByText("Localization"));
            }
        }
        public Link PermissionsTabLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByText("Permissions"));
                }
                return ContentPane.Link(Find.ByText("Permissions"));
            }
        }
        public Link AdvancedSettingsTabLink
        {
            get
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByText("Advanced Settings"));
                }
                return ContentPane.Link(Find.ByText("Advanced Settings"));
            }
        }
        public Link UploadIconFileLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ById(s => s.EndsWith("ManageTabs_ctlIcon_cmdUpload")));
                }
                return IEInstance.Link(Find.ById(s => s.EndsWith("ManageTabs_ctlIcon_cmdUplaod")));
            }
        }
        public Link IconUploadSelectedFileLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ById(s => s.EndsWith("Managetbas_ctlIcon_cmdSave")));
                }
                return IEInstance.Link(Find.ById(s => s.EndsWith("Managetbas_ctlIcon_cmdSave")));
            }
        }
        #endregion

        #region Divs
        public Div UserPermissionsDiv
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Div(Find.ById("dnn_ctr_ManageTabs_permissionRow"));
                }
                return IEInstance.Div(Find.ById("dnn_ctr_ManageTabs_permissionRow"));
            }
        }
        #endregion

        #region Tables
        public Table RolePermissionsTable
        {
            get { return UserPermissionsDiv.Tables[0];}
        }
        public Table UserPermissionsTable
        {
            get { return UserPermissionsDiv.Tables[1]; }
        }
        #endregion

        #region Images
        public Image AdvancedSettingsExpandButton
        {
            get { return IEInstance.Image(Find.ById(s => s.EndsWith("ManageTabs_dshAdvanced_imgIcon"))); }
        }
        
        #endregion

        #region RadioButtons
        public RadioButton LinkURLTypeRadioButton
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.RadioButton(Find.ById(s => s.EndsWith("ctr_ManageTabs_ctlURL_optType_1")));
                }
                return IEInstance.RadioButton(Find.ById(s => s.EndsWith("ctr_ManageTabs_ctlURL_optType_1")));
            }
        }
        public RadioButton NewCopyRadioButton
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.RadioButton(Find.ByValue("optNew"));
                }
                return IEInstance.RadioButton(Find.ByValue("optNew"));
            }
        }
        public RadioButton CopyRadioButton
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.RadioButton(Find.ByValue("optCopy"));
                }
                return IEInstance.RadioButton(Find.ByValue("optCopy"));
            }
        }
        public RadioButton ReferencedCopyRadioButton
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.RadioButton(Find.ByValue("optReference"));
                }
                return IEInstance.RadioButton(Find.ByValue("optReference"));
            }
        }
        public RadioButton LinkToPageRadioButton
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.RadioButton(Find.ById(s => s.EndsWith("_ManageTabs_ctlURL_optType_2")));
                }
                return IEInstance.RadioButton(Find.ById(s => s.EndsWith("_ManageTabs_ctlURL_optType_2")));
            }
        }
        public RadioButton InsertAfterRadioButton
        {
            get { return IEInstance.RadioButton(Find.ByValue("After")); }
        }
        public RadioButton LinkToFileRadioButton
        {
            get { return IEInstance.RadioButton(Find.ById(s => s.EndsWith("ctr_ManageTabs_ctlURL_optType_3"))); }
        }
        #endregion

        #region File Upload
        public FileUpload PageIconFileUpload
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.FileUpload(Find.ById(s => s.EndsWith("ManageTabs_ctlIcon_txtFile")));
                }
                return IEInstance.FileUpload(Find.ById(s => s.EndsWith("ManageTabs_ctlIcon_txtFile")));
            }
        }
        #endregion

        #endregion

        #region Public Methods

        public void ClickViewPermissionForRole(string roleName)
        {
            Image result = null;
            foreach (TableRow row in RolePermissionsTable.TableRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(roleName))
                {
                    result = row.TableCells[1].Image(Find.Any);
                    break;
                }
                continue;
            }
            result.ClickNoWait();
        }

        public void ClickEditPermissionForRole(string roleName)
        {
            CheckBox result = null;
            foreach (TableRow row in RolePermissionsTable.TableRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(roleName))
                {
                    result = row.TableCells[2].CheckBox(Find.Any);
                    break;
                }
                continue;
            }
            result.Click();
        }

        public void GiveUserEditPermissionsForPage(string userName, string displayName)
        {
            UserPermissionField.Value = userName;
            AddUserPermissionLink.Click();
            System.Threading.Thread.Sleep(1000);
            Image result = null;
            foreach (TableRow row in UserPermissionsTable.TableRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(displayName))
                {
                    result = row.TableCells[2].Image(Find.Any);
                    break;
                }
                continue;
            }
            result.Click();
        }
        public void GiveUserViewPermissionsForPage(string userName, string displayName)
        {
            UserPermissionField.Value = userName;
            AddUserPermissionLink.Click();
            System.Threading.Thread.Sleep(1000);
            Image result = null;
            foreach (TableRow row in UserPermissionsTable.TableRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(displayName))
                {
                    result = row.TableCells[1].Image(Find.Any);
                    break;
                }
                continue;
            }
            result.Click();
        }

        public bool GetViewPermissionsForRole(string roleName)
        {
            CheckBox result = null;
            foreach (TableRow row in RolePermissionsTable.TableRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(roleName))
                {
                    result = row.TableCells[1].CheckBox(Find.Any);
                    break;
                }
                continue;
            }
            bool canView = result.GetAttributeValue("Value").CompareTo("True") == 0;
            return canView;
        }

        public bool GetEditPermissionsForRole(string roleName)
        {
            CheckBox result = null;
            foreach (TableRow row in RolePermissionsTable.TableRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(roleName))
                {
                    result = row.TableCells[2].CheckBox(Find.Any);
                    break;
                }
                continue;
            }
            bool canView = result.GetAttributeValue("Value").CompareTo("True") == 0;
            return canView;
        }

        public void AddPage(string pageName)
        {
            PageNameField.Value = pageName;
            AddPageLink.ClickNoWait();
            System.Threading.Thread.Sleep(3000);
        }

        public void AddHiddenPage(string pageName)
        {
            PageNameField.Value = pageName;
            IncludeInMenuCheckbox.Checked = false;
            AddPageLink.ClickNoWait();
            System.Threading.Thread.Sleep(3000);
        }

        public void AddPageAfter(string pageName, string afterPage)
        {
            PageNameField.Value = pageName;
            InsertAfterRadioButton.Checked = true;
            InsertPageSelectList.Select(afterPage);
            AddPageLink.ClickNoWait();
            System.Threading.Thread.Sleep(3000);
        }

        public void AddChildPage(string childName, string parentName)
        {
            PageNameField.Value = childName;
            ParentPageSelection.Select(parentName);
            AddPageLink.ClickNoWait();
            System.Threading.Thread.Sleep(3000);
        }

        public void AddPageSelectTemplate(string pageName, string template)
        {
            
            PageNameField.Value = pageName;
            if (TemplateFolderSelectList.Option("Templates/").Exists)
            {
                TemplateFolderSelectList.Select("Templates/");
            }
            else
            {
                TemplateFolderSelectList.Select("Root");
            }
            PageTemplateSelect.Select(template);
            AddPageLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
        }

        public void AddPageSelectTemplateAllUsersView(string pageName, string template)
        {

            PageNameField.Value = pageName;
            if (TemplateFolderSelectList.Option("Templates/").Exists)
            {
                TemplateFolderSelectList.Select("Templates/");
            }
            else
            {
                TemplateFolderSelectList.Select("Root");
            }
            PageTemplateSelect.Select(template);
            ClickViewPermissionForRole("All Users");
            AddPageLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);
        }

        public void AddPageGiveUserViewPermission(string pageName, string userName)
        {
            PageNameField.Value = pageName;
            UserPermissionField.Value = userName;
            AddUserPermissionLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
            AddPageLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);
        }

        public void AddPageGiveUserViewPermissionSelectTemplate(string pageName, string userName, string templateName)
        {
            PageNameField.Value = pageName;
         
            //if (TemplateFolderSelectList.Option(@"Templates/").Exists)
            //{
            //    TemplateFolderSelectList.Select(@"Templates/");
            //}
            //else
            //{
              //  TemplateFolderSelectList.Select("Root");
            //}
            //TemplateFolderSelectList.Select("Templates/");
            PageTemplateSelect.Select(templateName);
            System.Threading.Thread.Sleep(1000);
            PermissionsTabLink.Click();
            UserPermissionField.Value = userName;
            AddUserPermissionLink.Click();
            System.Threading.Thread.Sleep(1000);
            AddPageLink.Click();
            System.Threading.Thread.Sleep(3000);
        }

        public void SelectTemplate(string templateFolder, string templateName)
        {
            TemplateFolderSelectList.Select(templateFolder);
            PageTemplateSelect.Select(templateName);
            UpdatePageLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
        }

        #endregion
    }
}
